package com.cheung.leetcode.hash;

import org.junit.jupiter.api.Test;

import java.util.*;

/**
 * @BelongsProject : java-leetcode
 * @BelongsPackage : com.cheung.leetcode.hash
 * @Author :  cheungming
 * @CreateTime : 2024-05-04 09:35:54
 * @Description : 349题
 * @Version : 1.0
 */
public class Code349Test {


    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();


        for (int i : nums1) {
            set1.add(i);
        }

        for (int i : nums2) {
            set2.add(i);
        }


        List<Integer> result = new ArrayList<>();
        for (Integer i : set1) {
            if (set2.contains(i)) {
                result.add(i);
            }
        }

        int[] res = new int[result.size()];
        for (int i = 0; i < result.size(); i++) {
            res[i] = result.get(i);
        }


        return res;
    }


    @Test
    public void test1() {
        int[] nums1 = {1, 2, 2, 1}, nums2 = {2, 2};
        assert Arrays.equals(intersection(nums1, nums2), new int[]{2});
    }

    @Test
    public void test2() {
        int[] num1 = {4, 9, 5}, nums2 = {9, 4, 9, 8, 4};
        assert Arrays.equals(intersection(nums2, num1), new int[]{4, 9})
                || Arrays.equals(intersection(nums2, num1), new int[]{9, 4});
    }
}
